package com.bj58.ecdata.dashboard.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.EnumPlatform;
import com.bj58.ecdata.dashboard.constant.TrafficFlowConst;
import com.bj58.ecdata.dashboard.databll.NetFlowBLL;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.dbentity.NetFlow;
import com.bj58.ecdata.dashboard.dbentity.NetFlowCity;
import com.bj58.ecdata.dashboard.dbentity.NetFlowSource;
import com.bj58.ecdata.dashboard.dbentity.SearchWord;
import com.bj58.ecdata.dashboard.dbentity.SearchWord_M;
import com.bj58.ecdata.dashboard.dbentity.SourceFlow;
import com.bj58.ecdata.dashboard.dbentity.TrafficFlow;
import com.bj58.ecdata.dashboard.dbentity.VisitPath;
import com.bj58.ecdata.dashboard.dbentity.VisitPathNew;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.ecdata.dashboard.utils.VelocityTools;
import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext.Model;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

/**
 * 
 * 流量
 * 
 * <p>
 * detailed comment
 * 
 * @author guolu 2015-1-25
 * @see
 * @since 1.0
 */
@Path("/netflow")
public class NetFlowController extends MvcController {
	private SomePublicUtil spu = new SomePublicUtil();
	private AbstractBaseService<NetFlow> nfService =ServiceBeanFactory.get(NetFlow.class,null);
	private AbstractBaseService<NetFlowCity> nfCityService = ServiceBeanFactory.get(NetFlowCity.class,null);
	private AbstractBaseService<SearchWord> searchWordService = ServiceBeanFactory.get(SearchWord.class,null);
	private AbstractBaseService<NetFlowSource> nfSourceService = ServiceBeanFactory.get(NetFlowSource.class,null);
	private AbstractBaseService<VisitPath> visitPathService = ServiceBeanFactory.get(VisitPath.class,null);
	private AbstractBaseService<VisitPathNew> visitPathNewService = ServiceBeanFactory.get(VisitPathNew.class,null);
	private static AbstractBaseService<TrafficFlow> trafficFlowService = ServiceBeanFactory.get(TrafficFlow.class,null);
	private static AbstractBaseService<SourceFlow> sourceFlowService = ServiceBeanFactory.get(SourceFlow.class,null);
	private VelocityTools vtool = new  VelocityTools();

	/**
	 * 流量-看板
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-25
	 */
	@Path({"/trend"})
	@Check("dashboard/netflow/trend")
	@GET
	@POST
	public ActionResult dashboard() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = nfService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
			List<List<String>> list = NetFlowBLL.getGeneral(DateUtil.strToDate(maxDate, "yyyy-MM-dd"), cateEnum);
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("yesterdayTable", list);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			context.put("cateTwoList",JSON.parseArray("["+cateEnum.getCateTwoNames()+"]"));
			context.put("cityOneList",EnumCate.getCityOneList());
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("netflow/dashboard");

	}

	/**
	 * 全站top 30城市
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path({ "/topNCity" })
	@Check("dashboard/netflow/topNCity")
	@GET
	@POST
	public ActionResult topNCity() {
		String returnURL = "netflow/topNCity";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);

			String plat = MyServletRequestUtils.getStringParameter(request, "platform", EnumPlatform.all.getColName());
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date", "");
			if(StringUtils.isBlank(stat_date)){
				stat_date = nfCityService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
//				DateUtil.getYestoday("yyyy-MM-dd");
			}
//			String month = DateUtil.getMonth(stat_date, "yyyy-MM-dd");
			model.add("topDataList", NetFlowBLL.getUVTop30City(cateEnum,platEnum, stat_date));
			model.add("platform", plat);
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("vtool", vtool);
			model.add("stat_date", stat_date);
			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	
	
	/** 全站搜索词top 30
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path({ "/topSearchWord" })
	@Check("dashboard/netflow/topSearchWord")
	@GET
	@POST
	public ActionResult topSearchWord() {
		String returnURL = "netflow/topSearchWord";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
//			if(StringUtils.isBlank(searchDate)){
//			SerializationUtils.clone(object);
//			searchDate = searchWordService.getMaxDate();
//				searchDate = DateUtil.getYestoday("yyyy-MM-dd");
//			}
			String searchDate=searchWordService.getMaxDate(String.format("business_name='%s'", EnumCate.valueOf(businessName).getDBName()));
			List<SearchWord> swordList_day = NetFlowBLL.getTopNSearchWords(searchDate,"day",businessName);
			List<SearchWord> swordList_week =  NetFlowBLL.getTopNSearchWords(searchDate,"week",businessName);
			List<SearchWord> swordList_month =  NetFlowBLL.getTopNSearchWords(searchDate,"month",businessName);
			
			List<SearchWord_M> swordList_day_M = NetFlowBLL.getTopNSearchWords_M(searchDate,"day",businessName);
			List<SearchWord_M> swordList_week_M =  NetFlowBLL.getTopNSearchWords_M(searchDate,"week",businessName);
			List<SearchWord_M> swordList_month_M =  NetFlowBLL.getTopNSearchWords_M(searchDate,"month",businessName);

			
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			
			model.add("swordList_day",swordList_day);
			model.add("swordList_week",swordList_week);
			model.add("swordList_month",swordList_month);
			
			model.add("swordList_day_M",swordList_day_M);
			model.add("swordList_week_M",swordList_week_M);
			model.add("swordList_month_M",swordList_month_M);
			
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("vtool", vtool);
			model.add("stat_date", DateUtil.strToStr(searchDate, "yyyy.MM.dd"));
			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	
	/**
	 * 流量来源数据
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path({ "/source" })
	@Check("dashboard/netflow/source")
	@GET
	@POST
	public ActionResult source() {
		String returnURL = "netflow/netflowSource";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			String maxDate = nfSourceService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("maxDate",maxDate);
			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	

	
	/**
	 * 用户访问路径
	 * @return
	 * @author zhaoxiang 2016-03-10
	 */
	@Path({ "/transform" })
	@Check("dashboard/netflow/transform")
	@GET
	@POST
	public ActionResult transform() {
		String returnURL = "netflow/transform";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			if(StringUtils.isBlank(stat_date)){
				stat_date=visitPathService.getMaxDate();
			}
//			String maxDate = nfSourceService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("stat_date",stat_date);
			model.add("platform",platform);
			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	

	
	/**
	 * 流量-看板, 测试页
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-04-24
	 */
	@Path({"/test"})
	@Check("dashboard/netflow/trend")
	@GET
	@POST
	public ActionResult dashboardTest() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = nfService.getMaxDate();
			List<List<String>> list = NetFlowBLL.getGeneral(DateUtil.strToDate(maxDate, "yyyy-MM-dd"), cateEnum);
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("yesterdayTable", list);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			context.put("cateTwoList",JSON.parseArray("["+cateEnum.getCateTwoNames()+"]"));
			context.put("cityOneList",EnumCate.getCityOneList());
			
			//来源表最大日期字段获取
			maxDate = nfSourceService.getMaxDate();
			context.put("maxDate",maxDate);
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("netflow/dashboard_test");

	}
	
	/**
	 *   流量来源
	 * 	
	 * @return
	 * @author wangchonglu
	 */
	@Path({"/sourceFlow"})
	@Check("dashboard/netflow/sourceFlow")
	@GET
	@POST
	public ActionResult sourceFlow() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			
			Map<String, Object> context = beat.getModel().getModel();
			
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			context.put("cate_CN_Map",TrafficFlowConst.businessCateMap);
			String date = DateUtil.getYestoday("yyyyMM");
			
			context.put("page_type_from",TrafficFlowConst.page_type_from);
			context.put("page_type_to",TrafficFlowConst.page_type_to);
			String maxDate = sourceFlowService.getMaxDateByTableSuffix("pc_source_"+date);
			//来源表最大日期字段获取
			context.put("maxDate",maxDate);
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			//一级来源和二级来源
			String source1 = NetFlowBLL.getCateByBusiness("pc","business_type_from","all",maxDate, "source");			
			context.put("source1",source1);
			return ActionResult.view("netflow/sourceFlow");
			
		} catch (Exception e) {
			return ActionResult.view("null");
		}
	

	}
	
	/**
	 * 流量传递与消失    
	 * 	
	 * @return
	 * @author wangchonglu
	 */
	@Path({"/trafficflow"})
	@Check("dashboard/netflow/trafficflow")
	@GET
	@POST
	public ActionResult trafficFlow() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			
			Map<String, Object> context = beat.getModel().getModel();
			
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			context.put("cate_CN_Map",TrafficFlowConst.businessCateMap);
			context.put("page_type_from",TrafficFlowConst.page_type_from);
			context.put("page_type_to",TrafficFlowConst.page_type_to);
			
			String date = DateUtil.getYestoday("yyyyMM");
			String maxDate = trafficFlowService.getMaxDateByTableSuffix("pc_cate_"+date);
				
			//来源表最大日期字段获取
			context.put("maxDate",maxDate);
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			return ActionResult.view("netflow/trafficFlow");
			
		} catch (Exception e) {
			return ActionResult.view("null");
		}
	

	}
	
	
	/**
	 * 用户访问路径(品类)
	 * @return
	 * @author wangchonglu
	 */
	@Path({ "/visitPL" })
	@Check("dashboard/netflow/visitPL")
	@GET
	@POST
	public ActionResult visitPL() {
		String returnURL = "netflow/visitPL";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			String cate =  MyServletRequestUtils.getStringParameter(request, "cate","");
			String actionType =  MyServletRequestUtils.getStringParameter(request, "actionType","");
			String plat =  MyServletRequestUtils.getStringParameter(request, "plat","");
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			if(StringUtils.isBlank(stat_date)){
				stat_date=visitPathNewService.getMaxDate();
			}
			if("total".equals(businessName)){
				
				model.add("cate_name_list",NetFlowBLL.getPathCate("cate_name","biz_name='all'"));
			}else{
				model.add("cate_name_list",NetFlowBLL.getPathCate("cate_name","biz_name='"+businessName+"'"));
			}
			model.add("source_list",NetFlowBLL.getPathCate("source",""));
//			String maxDate = nfSourceService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("stat_date",stat_date);
			model.add("platform",platform);
			model.add("cate",cate);
			model.add("actionType",actionType);
			model.add("plat",plat);

			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	
	
	/**
	 * 用户访问路径(来源)
	 * @return
	 * @author wangchonglu
	 */
	@Path({ "/visitSource" })
	@Check("dashboard/netflow/visitSource")
	@GET
	@POST 
	public ActionResult visitSource() {
		String returnURL = "netflow/visitSource";
		try{
			Model model = beat.getModel();
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","pc");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			String cate =  MyServletRequestUtils.getStringParameter(request, "cate","");
			String actionType =  MyServletRequestUtils.getStringParameter(request, "actionType","");
			String plat =  MyServletRequestUtils.getStringParameter(request, "plat","");
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			if(StringUtils.isBlank(stat_date)){
				stat_date=visitPathNewService.getMaxDate();
			}
			String biz = businessName;
			if("total".equals(biz)){
				biz = "all";
			}
			
			model.add("source_list",NetFlowBLL.getPathCate("source","biz_name='"+biz+"' AND platform='"+platform+"'"));
//			String maxDate = nfSourceService.getMaxDate(String.format("business_name='%s'", cateEnum.getDBName()));
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("stat_date",stat_date);
			model.add("platform",platform);
			model.add("cate",cate);
			model.add("actionType",actionType);
			model.add("plat",plat);
			return ActionResult.view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view(returnURL);
		}
	}
	
	
	
}
